#include <string.h>
#include <iostream>
using std::cout;
using std::endl;
class String
{
public:
	String()
    : _str(new char[1]()) 
    {}
    /* String(){} */

	String(const char* pstr)
    :_str(new char[strlen(pstr) + 1]()) 
    {
        cout << "String(const char *)" << endl;
		strcpy(_str, pstr);
	}

	//拷贝构造
	String(const String& rhs) 
    :_str(new char[strlen(rhs.c_str()) + 1]())
	{
        cout << "String(const String &)" << endl;
		strcpy(_str, rhs.c_str());
	}

    //移动构造
    String(String && rhs)
    : _str(rhs._str)
    {
        cout << "String(String&&)" << endl;
        rhs._str = nullptr;
    }

    //移动赋值函数
    String & operator=(String && rhs)
    {
        cout << "String & operator=(String&&)" << endl;
        if(this != &rhs){
            delete [] _str;
            //浅拷贝
            _str = rhs._str;
            /* _str = new char[1](); */
            rhs._str = nullptr;
        }
        return *this;
    }


    //默认拷贝构造
    /* String(const String & rhs) */
    /* : _str(rhs._str) */
    /* {} */

    //赋值运算符函数
	String &operator=(const String &rhs){
		if (this != &rhs)
		{
			delete [] _str;
		    _str = new char[strlen(rhs.c_str()) + 1];
		    strcpy(_str, rhs.c_str());
            cout << "String& operator=(const String &)" << endl;
		}
		return *this;
	}

	~String(){
		if (_str)
		{
			delete [] _str;
			_str = nullptr;
		}
        cout << "~String()" << endl;
	}

	void print(){
		cout << _str << endl;
	}

	size_t length() const{
		return	strlen(_str);
	}

	const char * c_str() const {
		return _str;
	}

private:
	char* _str;
};

void test0(){
    //用hello创建一个临时的string对象
    //是一个右值
    //然后调用拷贝构造，将这个右值作为参数传入
    String s1 = "hello";
    String s2(s1);

    String s3("hello");
    s3 = String("wangdao");

    s2 = s3;

    String("wangdao") = String("wangdao");
}

void test1(){
    String s1("hello");
    cout << &s1 << endl;
    /* &(std::move(s1)); */
    /* String && ref = s1; */
    /* String && ref = std::move(s1); */
    String s2 = std::move(s1);
    s2.print();

   cout << &s1 << endl;
    s1.print();
    cout << "over" << endl;
}

void test2(){
    int a = 1;
    cout << &a << endl;
    int && ref = std::move(a);
    /* &(std::move(a)); */
    cout << &ref << endl;
    ref = 10;
    cout << a << endl;
    cout << &a << endl;
}

void test3(){
    String s1("hello");
    //String && rhs = std::move(s1);
    //&s1 == &rhs;
    s1 = std::move(s1);
    s1.print();
    cout << "over" << endl;
    /* &(std::move(s1)); */
}

void test4(){
    String s1("hello");
    /* s1 = String("world"); */
    /* s1.print(); */

    s1 = std::move(s1);
    s1.print();
}

//探讨右值引用
String func2(){
    String str1("wangdao");
	str1.print();
    return str1;
}

void test5(){
    /* func2(); */
    //&func2(); //error,右值
    String && ref = func2();
    &ref;  //右值引用本身为左值
}

String s10("cpp");
String func3(){
    s10.print();
    return s10;
}

void test6(){
    /* &func3(); */
    String && ref = func3();
}



int main(void){
    test6();
    return 0;
}
